package com.fenghongzhang.lib_network.di


import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object HttpMoudle {

    //13, 提供了一个retrofit
    @Singleton
    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:7015/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build()
    }

    //14, 提供了一个okhttp
    @Singleton
    @Provides
    fun provideOkhttpClient(): OkHttpClient {
        val httpLoggingInterceptor = okhttp3.logging.HttpLoggingInterceptor()
        httpLoggingInterceptor.level = okhttp3.logging.HttpLoggingInterceptor.Level.BODY

        return OkHttpClient.Builder()
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .addInterceptor(httpLoggingInterceptor)
            .addInterceptor(HttpHeaderInterceptor())
            .build()
    }

}